import json, functools, time
from pymud import Trigger


class Configuration:
    def __init__(self, session, *args, **kwargs):
        self.session = session
        self.ws = session.application.get_globals("ws")
        # self._triggers = {}
        # self._initTriggers()
        self._objs = [
            # Trigger(
            #     self.session,
            #     id="tri_sc_pot",
            #     group="sys",
            #     patterns=r".*│\s*【潜能】\s*(\d+)\s+│",
            #     onSuccess=self.tri_sc_pot,
            # ),
            Trigger(
                self.session,
                id="tri_sc_exp",
                group="sys",
                patterns=r".*│\s*【经验】\s*(\d+)\s+│",
                onSuccess=self.tri_sc_exp,
            ),
            Trigger(
                self.session,
                r"^目前权限：\(player\)",
                onSuccess=self.tri_logined,
                group="sys",
                id="tri_logined",
            ),
            Trigger(
                self.session,
                r"^重新连线完毕。",
                onSuccess=functools.partial(self.tri_reLogin, relogin=True),
                group="sys",
                id="tri_relogin",
            ),
            Trigger(
                self.session,
                id="tri_fullme",
                group="sys",
                patterns=r"^http://fullme.pkuxkx.net/robot.php.+$",
                onSuccess=self.tri_fullme,
            ),
            Trigger(
                self.session,
                id="tri_get_fullme",
                group="get_fullme",
                patterns=r"^也许是上天垂青于你的勤奋和努力吧，一个小时之内你不用担心被判为机器人",
                onSuccess=self.tri_succ_fullme,
            ),
            # 获取姓名：触发
            # Trigger(
            #     self.session,
            #     id="tri_sc_name",
            #     group="sys",
            #     patterns=r"^│\s+(.+)\s+(.+)\(\w+\)\s+│",
            #     onSuccess=self.tri_sc_name,
            # ),
        ]

    """
    /**
     * 触发回调函数
     * @param {*} name 触发器名称
     * @param {*} line 原始消息
     * @param {*} wildcards 匹配到的结果
     */
    """

    # exp
    def tri_sc_exp(self, name, line, wildcards):
        self.session.combat_exp = wildcards[0]
        sendDatas = {
            "type": "sc_exp",
            "sc_exp": {
                "combat_exp": wildcards[0],
            },
        }
        self.ws.ws.send(json.dumps(sendDatas))

    # 潜能
    def tri_sc_pot(self, name, line, wildcards):
        sendDatas = {
            "type": "sc_pot",
            "sc_pot": {"pot": wildcards[0]},
        }
        self.ws.ws.send(json.dumps(sendDatas))

    # 获取姓名：回调函数
    def tri_sc_name(self, name, line, wildcards):
        sendDatas = {
            "type": "sc_name",
            "sc_name": {"menpai": wildcards[0], "name": wildcards[1]},
        }
        self.ws.ws.send(json.dumps(sendDatas))

    # 重连
    def tri_reLogin(self, name, line, wildcards, relogin=False):
        "登录处置"

    # 登录
    def tri_logined(self, name, line, wildcards):
        self.session.exec_command_after(2, "hp *;#wa 5000;hp")

    def tri_rumor(self, name, line, wildcards):
        self.session.info(wildcards[0])

    def tri_chat(self, name, line, wildcards):
        self.session.info(wildcards[0])

    def tri_succ_fullme(self, name, line, wildcards):
        sendDatas = {"type": "succ_fullme", "succ_fullme": 1}
        self.ws.ws.send(json.dumps(sendDatas))

    def tri_get_fullme(self, name, line, wildcards):
        self.session.info("补状态")
        self.session.enableGroup("haha", False)
        self.session.exec("fullme")

    def tri_fullme(self, name, line, wildcards):
        sendDatas = {"type": "get_fullme", "get_fullme": line}
        self.ws.ws.send(json.dumps(sendDatas))

    # haha测试
    def ontri_haha(self, name, line, wildcards):
        self.session.info("触发了haha")
        # 写入log
        # mylog = self.session.getLogger('mylog')
        # mylog.log('something you want to log')
        pass

    def __unload__(self):
        # self.session.delObjects(self._triggers)
        self.session.delObjects(self._objs)
